package com.retain.system.shiro;

import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author: LiuWei
 * @date: Create in 14:58 2018/11/26
 */
@Component
public class SpringEventListener {

    /**
     * 必须这样设置，否则spring 事务将失效
     *
     * @param event
     */
    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        ApplicationContext context = event.getApplicationContext();
        DefaultWebSecurityManager manager = (DefaultWebSecurityManager) context.getBean("securityManager");
        AuthorizingRealm realm = (AuthorizingRealm) context.getBean("myShiroRealm");
        manager.setRealm(realm);

    }
}
